package com.ocom.human.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityResourceMenu;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.human.dao.IDaoResourceMenu;
import com.ocom.human.service.ResourceMenuService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResourceMenuServiceImpl extends ServiceImpl<IDaoResourceMenu, EntityResourceMenu> implements ResourceMenuService {

    @Autowired
    private IDaoResourceMenu iDaoResourceMenu;

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityResourceMenu> getSaasMenuList(Integer saasNo,Integer ifCustom,Integer role) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("saas_no",saasNo);
        queryWrapper.eq("status",1);
        if((ifCustom==null || ifCustom==0) && role != 0 && role != 1){
            queryWrapper.eq("if_custom",0);
        }

        queryWrapper.orderByAsc("parent_id","sort");
        return iDaoResourceMenu.selectList(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean InsertSaasMenu(Integer saasNo, String saasName) {
        EntityResourceMenu entityResourceMenu=new EntityResourceMenu();
        entityResourceMenu.setSaasNo(saasNo);
        entityResourceMenu.setParentId(0);
        entityResourceMenu.setType((byte) 0);
        entityResourceMenu.setName(saasName);
        Date date=new Date();
        entityResourceMenu.setCreateTime(date);
        entityResourceMenu.setUpdateTime(date);
        return this.save(entityResourceMenu);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu getSaasMenu(Integer saasNo, Integer menuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("saas_no",saasNo);
        queryWrapper.eq("parent_id",menuId);
        queryWrapper.last(" limit 1");
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu getMenuId(Integer menuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",menuId);
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Boolean removeSaasMenu(Integer menuId) {
        return this.removeById(menuId);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateResourceMenus(EntityResourceMenu entityResourceMenus) {
        UpdateWrapper<EntityResourceMenu> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("sort = sort-1");
        updateWrapper.eq("parent_id",entityResourceMenus.getParentId());
        updateWrapper.eq("type",entityResourceMenus.getType());
        updateWrapper.gt("sort",entityResourceMenus.getSort());
        this.update(updateWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu getSaasMenus(EntityResourceMenu entityResourceMenu) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("type",entityResourceMenu.getType());
        queryWrapper.eq("parent_id",entityResourceMenu.getParentId());
        queryWrapper.lt("sort",entityResourceMenu.getSort());
        queryWrapper.orderByDesc("sort");
        queryWrapper.last(" LIMIT 1");
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateResourceMenu(EntityResourceMenu entityResourceMenu,Integer Id) {
        UpdateWrapper<EntityResourceMenu> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("sort = sort+1");
        updateWrapper.eq("id",entityResourceMenu.getId());
        this.update(updateWrapper);
        UpdateWrapper<EntityResourceMenu> updateWrappers=new UpdateWrapper<>();
        updateWrappers.set("sort",entityResourceMenu.getSort());
        updateWrappers.eq("id",Id);
        this.update(updateWrappers);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu getGtSaasMenus(EntityResourceMenu entityResourceMenu) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",entityResourceMenu.getType());
        queryWrapper.eq("parent_id",entityResourceMenu.getParentId());
        queryWrapper.gt("sort",entityResourceMenu.getSort());
        queryWrapper.orderByDesc("sort");
        queryWrapper.last(" LIMIT 1");
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateMoveDownSaasMenu(EntityResourceMenu entityResourceMenus, Integer id) {
        UpdateWrapper<EntityResourceMenu> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("sort = sort+1");
        updateWrapper.eq("id",entityResourceMenus.getId());
        this.update(updateWrapper);
        UpdateWrapper<EntityResourceMenu> updateWrappers=new UpdateWrapper<>();
        updateWrappers.set("sort",entityResourceMenus.getSort());
        updateWrappers.eq("id",id);
        this.update(updateWrappers);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu parentMenu(Integer saasNo, Integer parentMenuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("saas_no",saasNo);
        queryWrapper.eq("id",parentMenuId);
        queryWrapper.eq("status",1);
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu entityResourceMenuName(String name, Integer parentMenuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("parent_id",parentMenuId);
        queryWrapper.last(" limit 1");
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu sortResourceMenuName(Integer type, Integer parentMenuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("MAX(sort) as sort");
//        queryWrapper.eq("type",type);
        queryWrapper.eq("parent_id",parentMenuId);
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean saveResourceMenuName(EntityResourceMenu entityResourceMenuName) {
        return this.save(entityResourceMenuName);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceMenu entityResourceMenuNames(String name, Integer parentMenuId, Integer menuId) {
        QueryWrapper<EntityResourceMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("parent_id",parentMenuId);
        queryWrapper.ne("id",menuId);
        queryWrapper.last(" limit 1");
        return iDaoResourceMenu.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean updateResourceMenuName(JSONObject jsonObject, JSONObject menuInfo) {
        UpdateWrapper<EntityResourceMenu> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("name",menuInfo.getString("name"));
        updateWrapper.set("icon",menuInfo.getString("icon"));
        updateWrapper.set("description",menuInfo.getString("description"));
        updateWrapper.set("page_id",menuInfo.getInteger("pageId"));
        updateWrapper.set("xf_ver_list",menuInfo.getString("xfVerList"));
        updateWrapper.set("mj_ver_list",menuInfo.getString("mjVerList"));
        updateWrapper.set("app_icon",menuInfo.getString("appIcon"));
        updateWrapper.set("if_custom",menuInfo.getByte("ifCustom")==null?0:menuInfo.getByte("ifCustom"));
        updateWrapper.set("update_time",new Date());
        updateWrapper.eq("id",jsonObject.getInteger("menuId"));
        updateWrapper.eq("saas_no",jsonObject.getInteger("saasNo"));
        return this.update(updateWrapper);
    }
}
